<?php

$root = dirname(__FILE__);
define('BASEPATH',$root.'/');
require_once $root.'/config.php';
require_once $root.'/function.php';
require_once $root.'/model.php';
require_once $root.'/phpCurl.php';

//获取dmm.co.jp信息的浏览器
$dmCurl = new phpCurl();
$dmCurl->config['proxy'] = $proxy.':8889';
$dmCurl->config['cookie'] = 'cookiedmmcojp';

//獲取 aventertainments 信息的瀏覽器
$atmCurl = new phpCurl();
$atmCurl->config['cookie'] = 'cookieaventms';

//獲取 supermm 信息的瀏覽器
$spmCurl = new phpCurl();
$spmCurl->config['cookie'] = 'cookiesupermm';
//$spmCurl->config['proxy'] = $proxy.':9990';
//$spmCurl->config['proxy'] = $proxy;


$model = new Model();

//需要的Tags
$allowTags = $model->getAllowTags();

//獲取中轉站的server信息
$baseServer = $model->getBaseServer();
initBaseServerInfo($baseServer);

//遍历上传影片的目录
$dateDirList = myScandir($baseServerInfo['uploadsDir']);
//var_dump($datedirlist['dir']);
if( !is_array($dateDirList['dir'])){
   die("\n==== The uploadDir is Empty Or is not Exists!=====\n");
}
foreach($dateDirList['dir'] as $dateVal){
  $date = basename($dateVal);
  if(! is_numeric($date)){
     echo $date;
     continue;
  }
  //判斷日期目錄文件傳輸是否OK
  if(!file_exists($dateVal.'/ok.txt')){
     continue;
  }
  //日期目錄
  $cateDirList = myScandir($dateVal);
  foreach($cateDirList['dir'] as $cateVal){
     $cate = basename($cateVal);
     if(! is_numeric($cate)){
        continue;
     }
     //分類目錄
     $fileList = myScandir($cateVal,array('mp4'));
     $mp4FileList = array();
//篩選AB上下集為A
     foreach($fileList['file'] as $k => $fileVal){
        $fileInfo = pathinfo($fileVal);
        $avkey = $fileInfo['filename'];
        $isAbKey = 0;
        $suf = substr($avkey,-1);
//echo $fileVal,"\n",$suf,"\n";
        if(in_array($suf,array('A','B'))){
           $isAbKey = 1;
           $avkey = substr($avkey,0,-1);
           $len = count($fileList['file']);
           for($i = $k+1;$i<$len;$i++){
              if(false !== stripos($fileList['file'][$i],$avkey)){
                 unset($fileList['file'][$i]);
                 $mp4FileList[] = $fileVal;
              }
           }
        }else{
           $mp4FileList[] = $fileVal;
        }
     }
//var_dump($mp4filelist);exit;
     foreach($mp4FileList as $k => $fileVal){
        //影音文件
        $fileInfo = pathinfo($fileVal);
        $avkey = $fileInfo['filename'];
        $isAbKey = 0;
        $suf = substr($avkey,-1);
        if(in_array($suf,array('A','B'))){
           $isAbKey = 1;
           $avkey = substr($avkey,0,-1);
        }
        //影音封面
        if($isAbKey){
            $fileValA = sprintf('%s/%sA.mp4',$cateVal,$avkey);
            $fileValB = sprintf('%s/%sB.mp4',$cateVal,$avkey);
            $imgVal = sprintf('%s/%sA-b.jpg',$cateVal,$avkey);
        }else{
            $imgVal = sprintf('%s/%s-b.jpg',$fileInfo['dirname'],$fileInfo['filename']);
        }
        //影音信息
        if(9 == $cate){
           //
           $prex = '店長推薦,';
           $mode = 0;
        }else{
           $prex = '';
           $mode = 1;
        }
        $avinfo = getAvinfoByavkey($avkey,$mode);
        //判斷封面是否存在
        if(!file_exists($imgVal) && isset($avinfo['cover'])){
           downImg($avinfo['cover'],$imgVal);
        }
        //是否存在
        if(isset($avinfo['title']) && file_exists($imgVal)){
           //存儲server 信息
           $storageServer = $model->getStorageServer();
           if( !isset($storageServer['serverid'])){
              die("\n=====The storageServer serverid is Empty!====\n");
           }
           $avinfo['bkey'] = '';
           if($isAbKey){
              $avinfo['avkey'] = $avkey.'A';
              $avinfo['bkey'] = $avkey.'B';
           }
           $avdata['title'] = $avinfo['title'];
           $avdata['avkey'] = $avinfo['avkey'];
           $avdata['avkeys'] = $avinfo['avkey'];
           $avdata['bkey'] = $avinfo['bkey'];
           $avdata['ismasked'] = $avinfo['ismasked'];
           $avdata['description'] = $avinfo['intro'];
           if($avinfo['tags']){
              $avinfo['tags'] = ','.$avinfo['tags'];
           }
           $avdata['tags'] = sprintf('%s%s,%s%s',$prex,$avinfo['publisher'],$avinfo['peoples'],$avinfo['tags']);
           $avdata['serverid'] = $storageServer['serverid'];
           $avdata['cid'] = $cate;
           
           //將信息增加到video表
           $model->processData($avdata);
           //將文件使用rsync異地備份
           if($isAbKey){
              $fileStatusA = rsyncFile($fileValA);
              $fileStatusB = rsyncFile($fileValB);
              $fileStatus = $fileStatusA && $fileStatusB;
              $avkey .= 'A';
//echo '$fileStatusA=',$fileStatusA,"\n",'$fileStatusB',$fileStatusB,"\n",'$fileStatus',$fileStatus,"\n";
           }else{
              $fileStatus = rsyncFile($fileVal);
           }
           $imgStatus = rsyncFile($imgVal);
           if($fileStatus && $imgStatus){
              //更新预发video
              $model->setAdvanceVideo($avkey,$date);
              //增加存儲serverid的weighted
              $model->setStorageServerWeight($storageServer['id']); 
           }else{
              //有个失败怎么处理?
              
           }
        }else{
           //noneinfo
           if($isAbKey){
               $noneFileValA = sprintf('%s/%s/%s/%sA.mp4',$baseServerInfo['noneInfoDir'],$date,$cate,$avkey);
               $noneFileValB = sprintf('%s/%s/%s/%sB.mp4',$baseServerInfo['noneInfoDir'],$date,$cate,$avkey);
               $noneImgVal = sprintf('%s/%s/%s/%sA-b.jpg',$baseServerInfo['noneInfoDir'],$date,$cate,$avkey);
               moveFile($fileValA,$noneFileValA);
               moveFile($fileValB,$noneFileValB);
           }else{
               $noneFileVal = sprintf('%s/%s/%s/%s',$baseServerInfo['noneInfoDir'],$date,$cate,$fileInfo['basename']);
               $noneImgVal = sprintf('%s/%s/%s/%s-b.jpg',$baseServerInfo['noneInfoDir'],$date,$cate,$fileInfo['filename']);
               moveFile($fileVal,$noneFileVal);
           }
           moveFile($imgVal,$noneImgVal);
        }
// usleep
     usleep(300000);
     }// end videofilelist
// 清空已處理的分類目錄
     @rmdir($cateVal);
  }//end catedir
//刪除OK文件
  @unlink($dateVal.'/ok.txt');
//清空已處理的日期目錄
  @rmdir($dateVal);
}// end uploaddir

echo "\n$avkey 自動化執行完畢!\n";

//計算磁盤使用情況
$serverList = $model->getStorageServerList();
foreach($serverList as $storageServer){
  $return = getDiskUseSize();
  if( !$return){
     continue;
  }
  $return['id'] = $storageServer['id'];
//var_dump($return);exit;
  $model->setStorageServerUse($return);
  usleep(300);
}

$cmd = sprintf("/bin/chmod -R 777 %s", $baseServerInfo['noneInfoDir']);
@system($cmd);
echo "\n =========== 計算磁盤空間使用情況結束! =============== \n";
sleep(60);
